package jsyf.oa.service.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.DateUtils;
import jsyf.oa.dao.system.RegionDao;
import jsyf.oa.dao.system.TargetDao;
import jsyf.oa.dao.system.UserDao;
import jsyf.oa.entity.system.TargetEntity;
import jsyf.oa.facade.system.TargetFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by WHH-PC on 2018/1/26.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class TargetService implements TargetFacade {

    @Autowired
    private TargetDao targetDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private RegionDao regionDao;

    @Override
    public String save(String jsonData) {
        TargetEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), TargetEntity.class);
        //删除此人本月目标
        Date day = targetDao.getCurrentTime();
        String nowMonth = DateUtils.parse(day, "yyyy-MM");
        targetDao.removeByCondition(" AND LEFT(TARGET_DATE,7) = ? AND COMPANY_ID = ? AND USER_ID = ? ", new String[]{nowMonth, entity.getCompanyId(), entity.getUserId()});
        Calendar calendar = Calendar.getInstance();
        //获取前月的第一天
        String firstDay = DateUtils.getFirstDayOfMonth();
        //获取当月天数
        Integer days = DateUtils.getLastDayOfMonth(calendar.get(calendar.YEAR), calendar.get(calendar.MONTH) + 1);
        //生成本月目标
        List<TargetEntity> targetEntityList = new ArrayList<>();
        for (Integer j = 1; j <= days; j++) {
            TargetEntity targetEntity = new TargetEntity();
            //某天目标
            String targetDate = firstDay.substring(0, 7) + "-" + (j < 10 ? "0" + j : j);
            targetEntity.setUserId(entity.getUserId());
            targetEntity.setTargetDate(targetDate);
            targetEntity.setCompanyId(entity.getCompanyId());
            targetEntity.setFaceTarget(entity.getFaceTarget());
            targetEntity.setTelTarget(entity.getTelTarget());
            targetEntity.setCreateUserId(entity.getCreateUserId());
            //查询销售的所在区域id
            JSONObject regionJson = regionDao.getRegionByRoleName(entity.getUserId(), " AND PARENT_ID != '0'");
            if (regionJson != null) {
                targetEntity.setRegionId(regionJson.get("id").toString());
            }
            //查询销售的大区id
            JSONObject bigRegionJson = regionDao.getRegionByRoleName(entity.getUserId(), " AND PARENT_ID = '0'");
            if (regionJson != null) {
                targetEntity.setBigRegionId(bigRegionJson.get("id").toString());
            }
            targetEntityList.add(targetEntity);
        }
        targetDao.insertBatch(targetEntityList);
        return entity.getId();
    }

    @Override
    public List<JSONObject> getTargetList(JSONObject jsonObject) {
        String companyId = jsonObject.get("companyId").toString();
        String targetDate = jsonObject.get("targetDate").toString();
        String userId = jsonObject.get("userId").toString();
        JSONObject userJson = userDao.getViewJSONById(userId);
        StringBuffer condition = new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();
        condition.append(" AND companyId = ?");
        sqlArgs.add(companyId);
        condition.append(" AND targetDate = ? ");
        sqlArgs.add(targetDate);
        condition.append(" AND jobState!=1 ");
        condition.append(" ORDER BY companyName asc, roleName desc,`name` ASC ");
        List<JSONObject> records = targetDao.getViewJSONListByCondition(condition.toString(), sqlArgs.toArray());
        return records;
    }

    @Override
    public JSONObject getById(String id) {
        return targetDao.getViewJSONById(id);
    }


    public List<JSONObject> getCompanyTarget() {
        return targetDao.getCompanyTarget();
    }
}
